package com.hud.easynavi;

import android.util.Log;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Formatter;

import de.greenrobot.event.EventBus;
import protocol.ProtoMsg;

/**
 * Created by Administrator on 2017/3/16.
 */
public class SerialPortReceiveThread extends Thread
        implements SerialPortMsgParser.OnFilterResultListener
{
    String TAG = "SerialPortReceiveThread";
    SerialPort serialPort;
    boolean running = true;
    byte[] buff = new byte[1024];
    SerialPortMsgParser parser; // 协议解析

    public SerialPortReceiveThread(){
    }

    public void openPort(){
        closePort();
        try {
            serialPort = new SerialPort(new File("/dev/ttymxc1"),115200,0);
        } catch (IOException e) {
            e.printStackTrace();
        }
        parser = new SerialPortMsgParser();
        parser.setLisenter(this);
    }

    public void closePort(){
        if(serialPort!=null){
            serialPort.close();
        }
    }

    @Override
    public void run() {
        while(running && !isInterrupted())
        {
            try {
                InputStream is = serialPort.getInputStream();
                int len = is.read(buff);
                Log.d(TAG,"read serialport data <--"+len+" bytes");
                logData(buff,len);
                parser.onDataFiltering(buff,len);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void stopRun(){
        running = false;
        closePort();
    }

    @Override
    public void OnMessageReceive(ByteBuffer data_buffer) {
        System.out.println("---- head:" +data_buffer.getShort());
        System.out.println("---- total_len:" +data_buffer.getInt());
        System.out.println("---- msg_type:" +data_buffer.getInt());
        System.out.println("---- msg_id:" +data_buffer.getLong());
        int action_len = data_buffer.getInt();
        System.out.println("---- action_len:" +action_len);
        byte[] str_byte= new byte[action_len];
        data_buffer.get(str_byte, 0, action_len);
        String action_string = new String(str_byte);
        System.out.println("action string:"+action_string);

        int data_len = data_buffer.getInt();
        System.out.println("---- data_len:" +data_len);
        byte[] data_byte= new byte[data_len];
        data_buffer.get(data_byte, 0, data_len);
        String dataString = new String(data_byte);
        System.out.println("data string:"+dataString);

        notifyProtoMessageReceived(action_string,dataString);
    }

    void notifyProtoMessageReceived(String act,String json){
        EventBus.getDefault().post(new ProtoMsg(act,json));
    }

    void logData(byte[] can_frame ,int len)
    {
        StringBuilder sb = new StringBuilder();
        System.out.println("len="+len);
        for (int i=0;i<len;i++)
        {
           sb.append(new Formatter().format("%02X ",can_frame[i]));
        }
        System.out.println("\n");
        System.out.println(" packet:"+sb.toString() + "\n");
    }
}
